/*
 * ActiveConnectionsJPanel.java
 */

package jrdesktop.server;

import java.net.InetAddress;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import jrdesktop.rmi.server.RMIServer;
import jrdesktop.viewer.Viewer;

/**
 *
 * @author  Benbac
 */
public class ActiveConnectionsJPanel extends javax.swing.JPanel {

    DefaultListModel listModel = new DefaultListModel();
    Hashtable<Integer, Integer> viewerKeys;
    
    /** Creates new form ActiveConnectionsJPanel */
    public ActiveConnectionsJPanel() {
        initComponents();
        jList1.setModel(listModel);
        updateList();        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonDisconnectAll = new javax.swing.JButton();
        jButtonRefresh = new javax.swing.JButton();
        jButtonDisconnect = new javax.swing.JButton();
        jButtonProperties = new javax.swing.JButton();
        jButtonDetails = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        jButtonDisconnectAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jrdesktop/images/connect_no.png"))); // NOI18N
        jButtonDisconnectAll.setText("Disconnect all");
        jButtonDisconnectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDisconnectAllActionPerformed(evt);
            }
        });

        jButtonRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jrdesktop/images/reload.png"))); // NOI18N
        jButtonRefresh.setText("Refresh");
        jButtonRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRefreshActionPerformed(evt);
            }
        });

        jButtonDisconnect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jrdesktop/images/connect_no.png"))); // NOI18N
        jButtonDisconnect.setText("Disconnect");
        jButtonDisconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDisconnectActionPerformed(evt);
            }
        });

        jButtonProperties.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jrdesktop/images/props.png"))); // NOI18N
        jButtonProperties.setText("Properties");
        jButtonProperties.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPropertiesActionPerformed(evt);
            }
        });

        jButtonDetails.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jrdesktop/images/info.png"))); // NOI18N
        jButtonDetails.setText("Details");
        jButtonDetails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDetailsActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jList1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 218, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jButtonDisconnectAll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jButtonRefresh, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jButtonDetails, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jButtonProperties, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jButtonDisconnect, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jButtonDisconnect)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonProperties)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonDetails)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonRefresh)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonDisconnectAll)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButtonDisconnectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDisconnectAllActionPerformed
    if (viewerKeys.size() == 0) return;
    if (JOptionPane.showConfirmDialog(null, "Disconnect viewers ? ", 
            "Confirm Dialog", JOptionPane.OK_CANCEL_OPTION, 
            JOptionPane.QUESTION_MESSAGE) == JOptionPane.CANCEL_OPTION) return;         
     
    if (RMIServer.serverConfig.reverseConnection)
        Viewer.disconnectAllViewers();
    else
        Server.disconnectAllViewers();
    updateList();
}//GEN-LAST:event_jButtonDisconnectAllActionPerformed

private void jButtonRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRefreshActionPerformed
    updateList();
}//GEN-LAST:event_jButtonRefreshActionPerformed

private void jButtonDisconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDisconnectActionPerformed
    int index = jList1.getSelectedIndex();
    if (index == -1) return;
    InetAddress inetAddress = (InetAddress) jList1.getSelectedValue(); 

    if (JOptionPane.showConfirmDialog(this, "Disconnect " + 
            inetAddress.toString() + " ? ", 
            "Confirm Dialog", JOptionPane.OK_CANCEL_OPTION, 
            JOptionPane.QUESTION_MESSAGE) == JOptionPane.CANCEL_OPTION) return;    
    
    if (RMIServer.serverConfig.reverseConnection)
        Viewer.removeViewer(viewerKeys.get(index));
    else
        Server.removeViewer(viewerKeys.get(index));
    updateList();
}//GEN-LAST:event_jButtonDisconnectActionPerformed

private void jButtonPropertiesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPropertiesActionPerformed
    int index  = jList1.getSelectedIndex();
    if (index == -1) return;            
    if (RMIServer.serverConfig.reverseConnection)
        Viewer.displayViewerProperties(viewerKeys.get(index));
    else
        Server.displayViewerProperties(viewerKeys.get(index));
}//GEN-LAST:event_jButtonPropertiesActionPerformed

private void jButtonDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetailsActionPerformed
    int index  = jList1.getSelectedIndex();
    if (index == -1) return;
    if (RMIServer.serverConfig.reverseConnection)
        Viewer.displayConnectionInfos(viewerKeys.get(index));
    else
        Server.displayConnectionInfos(viewerKeys.get(index));
}//GEN-LAST:event_jButtonDetailsActionPerformed

    public void updateList() {       
        Hashtable<Integer, InetAddress> hosts;
        if (RMIServer.serverConfig.reverseConnection)
            hosts = Viewer.getConnectedHosts();
        else
            hosts = Server.getConnectedHosts();
        
        Enumeration<Integer> viewersKeys = hosts.keys();
        viewerKeys = new Hashtable<Integer, Integer>();
        int i = 0;
        listModel.clear();

        while (viewersKeys.hasMoreElements()) {
            int key = viewersKeys.nextElement();
            viewerKeys.put(i++, key);
            listModel.addElement(hosts.get(key));
        }
    } 
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonDetails;
    private javax.swing.JButton jButtonDisconnect;
    private javax.swing.JButton jButtonDisconnectAll;
    private javax.swing.JButton jButtonProperties;
    private javax.swing.JButton jButtonRefresh;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
